<template>
  <div class="com-container">
    <div class="title"
         :style="titleStyle">
      <span>▎ {{showTitle}}</span>
      <span class="iconfont icon-arrow-down title-icon"
            @click="showChoice = !showChoice"
            :style="titleStyle"></span>
      <div class="select-con"
           v-show="showChoice">
        <div class="select-item"
             v-for="item in selectTypes"
             :key="item.key"
             @click="handleSelect(item.key)">
          {{item.text}}
        </div>
      </div>
    </div>
    <div class="com-chart"
         ref="trendRef">

    </div>
  </div>
</template>

<script>
import { getTrendData } from 'network/trend.js';
export default {
  name: 'Trend',
  data () {
    return {
      echartsInstance: null,
      allData: null, //服务器返回的数据
      showChoice: false, //是否显示可选项
      choiceType: 'map', //选择的类型
      titleFontSize: 0, //标题的字体大小
    }
  },
  mounted () {
    this.initChart();
    this.getData();
    this.adaptScreen();
    window.addEventListener('resize', this.adaptScreen);
  },
  destroyed () {
    window.removeEventListener('resize', this.adaptScreen);
  },
  computed: {
    //可选择的类型
    selectTypes () {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type.filter(item => {
          return item.key !== this.choiceType; //可选择的标题不包括当前已选择的标题
        });
      }
    },
    //当前展示的标题
    showTitle () {
      if (!this.allData) {
        return [];
      } else {
        return this.allData[this.choiceType].title; //根据当前选择的类型确定标题
      }
    },
    //标题样式
    titleStyle () {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    }
  },
  methods: {
    //初始化echarts实例化对象
    initChart () {
      this.echartsInstance = this.$echarts.init(this.$refs.trendRef, 'chalk');
      //对图表初始化配置的控制
      const initOption = {
        xAxis: {
          type: 'category',
          boundaryGap: false, //坐标轴两边不留白
        },
        yAxis: {
          type: 'value'
        },
        grid: {
          top: '35%',
          left: '3%',
          right: '6%',
          bottom: '5%',
          containLabel: true //grid 区域包含坐标轴的刻度标签
        },
        //提示框组件
        tooltip: {
          trigger: 'axis', //坐标轴触发,
        },
        //图例
        legend: {
          left: 85,
          top: '20%',
          icon: 'circle'
        }
      };
      this.echartsInstance.setOption(initOption);
    },

    //获取服务器的数据
    async getData () {
      const { data: res } = await getTrendData();
      this.allData = res;
      console.log(this.allData);
      this.updateChart();
    },

    //更新图表
    updateChart () {
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      const timeArr = this.allData.common.month;
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          name: item.name,
          data: item.data,
          stack: this.choiceType, //堆叠图 
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index]
              }, // %0的颜色值
              {
                offset: 1,
                color: colorArr2[index]
              } // 100%的颜色值
            ])
          }
        }
      });
      //图例数据
      const legendArr = valueArr.map(item => {
        return item.name
      })
      const dataOption = {
        xAxis: {
          data: timeArr
        },
        series: seriesArr,
        legend: {
          data: legendArr
        },
      };
      this.echartsInstance.setOption(dataOption);

    },

    //浏览器窗口发生变化，调用
    adaptScreen () {
      this.titleFontSize = this.$refs.trendRef.offsetWidth / 100 * 3.6;
      //和分辨率大小相关的配置项
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize / 1.5,
          itemHeight: this.titleFontSize / 1.5,
          itemGap: this.titleFontSize / 1.5,
          textStyle: {
            fontSize: this.titleFontSize / 1.5
          }
        }
      };
      this.echartsInstance.setOption(adapterOption);
      this.echartsInstance.resize();
    },

    //切换标题选择时触发
    handleSelect (currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    }

  },
}
</script>

<style lang="less" scoped>
@import "~assets/font/iconfont.css";
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: #fff;

  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }

  .select-con {
    margin-left: 1em;
    background-color: #222733;

    .select-item {
      cursor: pointer;
    }
  }
}
</style>